package homework;

import java.util.*;

public class leetcode37 {
    List<List<Integer>>list=new ArrayList<>();
    public List<List<Integer>> levelOrder(TreeNode root) {
        if(root==null){
            return list;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        int count=0;
        while (!queue.isEmpty()){
            count=queue.size();
            List<Integer>list1=new ArrayList<>();
            for (int i = 0; i < count; i++) {
                TreeNode treeNode=queue.poll();
                list1.add(treeNode.val);
                if(treeNode.left!=null) queue.add(treeNode.left);
                if(treeNode.right!=null) queue.add(treeNode.right);
            }
            list.add(list1);
        }
        return list;
    }
}
